package com.ruoyi.task.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Title
 * @Description
 * @author   liyan
 * @param
 * @return
 * @date 2021/7/20 10:04
 */
@Component
public class WishuXxlJob {
    private static Logger logger = LoggerFactory.getLogger(WishuXxlJob.class);

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    RestHighLevelClient restHighLevelClient;


    private SimpleDateFormat  simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 1、简单任务示例（Bean模式）
     */
    @XxlJob("logGob")
    public void demoJobHandler() throws Exception {

        String format = simpleDateFormat.format(new Date());
        System.out.println("格式化时间："+format);

        try {
            Calendar c = Calendar.getInstance();
            c.setTime(simpleDateFormat.parse(format));
            c.add(Calendar.DATE,-30);
            Date time = c.getTime();
            System.out.println("time："+time);
            String format1 = simpleDateFormat.format(time);
            System.out.println("format1："+format1);


            GetIndexRequest exist = new GetIndexRequest(format1);
            boolean exists = restHighLevelClient.indices().exists(exist, RequestOptions.DEFAULT);
            //true 超过30天
            if(exists){
                DeleteIndexRequest request = new DeleteIndexRequest(format1);
                //设置超时
                request.timeout(TimeValue.timeValueMinutes(2));
                AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
                if(delete.isAcknowledged()){
                    System.out.println("清除日志");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        System.out.println("===================execort  job========");

    }


    /**
     * 2、分片广播任务
     */
    //@XxlJob("shardingJobHandler")
    public void shardingJobHandler() throws Exception {

        // 分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();

        XxlJobHelper.log("分片参数：当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal);

        // 业务逻辑
        for (int i = 0; i < shardTotal; i++) {
            if (i == shardIndex) {
                XxlJobHelper.log("第 {} 片, 命中分片开始处理", i);
            } else {
                XxlJobHelper.log("第 {} 片, 忽略", i);
            }
        }

    }



    @XxlJob("testJob")
    public void testJob(){
        System.out.println(new Date());
    }


}
